package com.oig.sys.msg.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.oig.common.constants.CommonEnum;
import com.oig.sys.msg.constants.MsgEnums;
import com.oig.sys.msg.dao.ITbNoticeMsgReceiverDao;
import com.oig.sys.msg.dto.NoticeMsgReceiverDto;
import com.oig.sys.msg.entity.TbNoticeMsg;
import com.oig.sys.msg.entity.TbNoticeMsgReceiver;
import com.oig.sys.msg.service.ITbNoticeMsgReceiverService;
import com.oig.sys.msg.vo.NoticeMsgReceiverVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wuxl
 */
@Slf4j
@Service
public class TbNoticeMsgReceiverServiceImpl implements ITbNoticeMsgReceiverService {

	private final ITbNoticeMsgReceiverDao tbNoticeMsgReceiverDao;

	@Autowired
	public TbNoticeMsgReceiverServiceImpl(ITbNoticeMsgReceiverDao tbNoticeMsgReceiverDao){
		 this.tbNoticeMsgReceiverDao = tbNoticeMsgReceiverDao;
	}


	private TbNoticeMsgReceiver initReceiver(TbNoticeMsg noticeMsg){
		TbNoticeMsgReceiver receiver = new TbNoticeMsgReceiver();
		receiver.setMsgId(noticeMsg.getId());
		receiver.setMsgStatus(MsgEnums.ReceiverMsgStatus.UN_READ.getCode());
		receiver.setCreateUserId(noticeMsg.getCreateUserId());
		receiver.setCreateUserName(noticeMsg.getCreateUserName());
		receiver.setTenantId(noticeMsg.getTenantId());
		receiver.setTenantName(noticeMsg.getTenantName());
		if (MsgEnums.MsgStatus.PUBLISH.getCode().equals(noticeMsg.getMsgStatus())){
			receiver.setSendStatus(MsgEnums.SendStatus.READY.getCode());
		} else {
			receiver.setSendStatus(MsgEnums.SendStatus.NO_DO.getCode());
		}
		receiver.setDelFlag(CommonEnum.FlagEnum.FALSE_FLAG.getCode());
		return receiver;
	}


	@Transactional
	@Override
	public void addReceiver(TbNoticeMsg noticeMsg, List<NoticeMsgReceiverDto> noticeMsgReceiverList) {
		for (NoticeMsgReceiverDto noticeMsgReceiverDto : noticeMsgReceiverList){
			TbNoticeMsgReceiver receiver = this.initReceiver(noticeMsg);
			receiver.setReceiverId(noticeMsgReceiverDto.getReceiverId());
			receiver.setReceiverName(noticeMsgReceiverDto.getReceiverName());
			this.tbNoticeMsgReceiverDao.save(receiver);
		}
	}


	@Transactional
	@Override
	public void updateReceiver(TbNoticeMsg noticeMsg, List<NoticeMsgReceiverDto> noticeMsgReceiverList) {
		List<Integer> idList = new ArrayList<>();
		for (NoticeMsgReceiverDto noticeMsgReceiverDto : noticeMsgReceiverList){
			TbNoticeMsgReceiver receiver = this.tbNoticeMsgReceiverDao.getByMsgIdAndReceiverId(noticeMsg.getId(), noticeMsgReceiverDto.getReceiverId());
			if (receiver==null){
				receiver = this.initReceiver(noticeMsg);
				receiver.setReceiverId(noticeMsgReceiverDto.getReceiverId());
				receiver.setReceiverName(noticeMsgReceiverDto.getReceiverName());
				this.tbNoticeMsgReceiverDao.save(receiver);
			}
			idList.add(receiver.getId());
		}
		List<Integer> delIdList = this.tbNoticeMsgReceiverDao.findAllDelId(noticeMsg.getId(), idList);
		if (delIdList!=null&&!delIdList.isEmpty()){
			this.tbNoticeMsgReceiverDao.deleteByIdList(delIdList);
		}
	}

    @Transactional
	@Override
	public void delByMsgId(Integer msgId) {
		List<Integer> idList = this.tbNoticeMsgReceiverDao.findAllIdByMsgId(msgId);
		if (CollectionUtil.isNotEmpty(idList)){
			this.tbNoticeMsgReceiverDao.deleteByIdList(idList);
		}
	}



	@Override
	public List<NoticeMsgReceiverVo> findMsgReceiver(Integer msgId) {
		return this.tbNoticeMsgReceiverDao.findAllByMsgId(msgId);
	}

	@Transactional
	@Override
	public void updateUserRead(Integer msgId, Integer userId) {
		TbNoticeMsgReceiver msgReceiver = this.tbNoticeMsgReceiverDao.getByMsgIdAndReceiverId(msgId, userId);
		msgReceiver.setMsgStatus(MsgEnums.ReceiverMsgStatus.READ.getCode());
		msgReceiver.setReadTime(new Date());
		this.tbNoticeMsgReceiverDao.save(msgReceiver);
	}

	@Transactional
	@Override
	public void updateUserRead(List<Integer> msgIdList, Integer userId) {
		List<TbNoticeMsgReceiver> msgReceiverList = this.tbNoticeMsgReceiverDao.findByMsgIdAndReceiverId(msgIdList, userId);
		for (TbNoticeMsgReceiver msgReceiver : msgReceiverList){
			msgReceiver.setMsgStatus(MsgEnums.ReceiverMsgStatus.READ.getCode());
			msgReceiver.setReadTime(new Date());
			this.tbNoticeMsgReceiverDao.save(msgReceiver);
		}
	}

	@Transactional
	@Override
	public void updateUserDel(Integer msgId, Integer userId) {
		TbNoticeMsgReceiver msgReceiver = this.tbNoticeMsgReceiverDao.getByMsgIdAndReceiverId(msgId, userId);
		msgReceiver.setDelFlag(CommonEnum.FlagEnum.TRUE_FLAG.getCode());
		this.tbNoticeMsgReceiverDao.save(msgReceiver);
	}

	@Transactional
	@Override
	public void updateUserDel(List<Integer> msgIdList, Integer userId) {
		List<TbNoticeMsgReceiver> msgReceiverList = this.tbNoticeMsgReceiverDao.findByMsgIdAndReceiverId(msgIdList, userId);
		for (TbNoticeMsgReceiver msgReceiver : msgReceiverList){
			msgReceiver.setDelFlag(CommonEnum.FlagEnum.TRUE_FLAG.getCode());
			this.tbNoticeMsgReceiverDao.save(msgReceiver);
		}
	}

	@Transactional
	@Override
	public void updateUserReadAll(Integer userId) {
		List<Integer> idList = this.tbNoticeMsgReceiverDao.findAllUnReadId(userId);
		if (CollectionUtil.isNotEmpty(idList)){
			this.tbNoticeMsgReceiverDao.updateUserRead(idList);
		}
	}

	@Transactional
	@Override
	public void updateUserDelAll(Integer userId) {
		List<Integer> idList = this.tbNoticeMsgReceiverDao.findAllUnDelId(userId);
		if (CollectionUtil.isNotEmpty(idList)){
			this.tbNoticeMsgReceiverDao.updateUserDel(idList);
		}
	}

}
